{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 基本数据类型\n",
    "* Python 中的变量不需要声明。每个变量在使用前都必须赋值，变量赋值以后该变量才会被创建。\n",
    "* 在 Python 中，变量就是变量，它没有类型，我们所说的\"类型\"是变量所指的内存中对象的类型。\n",
    "* 等号（=）用来给变量赋值。\n",
    "* 等号（=）运算符左边是一个变量名,等号（=）运算符右边是存储在变量中的值。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": "1 2 字符串111\n"
    }
   ],
   "source": [
    "xa=1\n",
    "xb=2\n",
    "xc='字符串111'\n",
    "print(xa,xb,xc)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 多个变量赋值\n",
    "Python允许你同时为多个变量赋值。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "a = b = c = 1"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "也可以为多个对象指定多个变量"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "a, b, c = 1, 2, \"hello\""
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 标准数据类型\n",
    "## Python3 中有六个标准的数据类型：\n",
    "* Number（数字）\n",
    "* String（字符串）\n",
    "* List（列表）\n",
    "* Tuple（元组）\n",
    "* Sets（集合）\n",
    "* Dictionary（字典）\n",
    "\n",
    "## Python3 的六个标准数据类型中：\n",
    "* 不可变数据（四个）：Number（数字）、String（字符串）、Tuple（元组）、Sets（集合）；\n",
    "* 可变数据（两个）：List（列表）、Dictionary（字典）。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Number（数字）\n",
    "* Python3 支持 int、float、bool、complex（复数）\n",
    "* 像大多数语言一样，数值类型的赋值和计算都是很直观的。\n",
    "* 内置的 type() 函数可以用来查询变量所指的对象类型。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'int'> <class 'float'> <class 'bool'> <class 'complex'>\n"
     ]
    }
   ],
   "source": [
    "a, b, c, d = 20, 5.5, True, 4+3j\n",
    "print(type(a), type(b), type(c), type(d))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "此外还可以用 isinstance 来判断"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "isinstance(a,int)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "isinstance(b,float)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# String（字符串）\n",
    "* Python中的字符串用单引号(')或双引号(\")括起来，同时使用反斜杠(\\\\)转义特殊字符。\n",
    "- 字符串的截取的语法格式如下：\n",
    "> 变量[头下标:尾下标]\n",
    "* 索引值以 0 为开始值，-1 为从末尾的开始位置。\n",
    "* 加号 (+) 是字符串的连接符， 星号 (*) 表示复制当前字符串，紧跟的数字为复制的次数。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "hello world\n",
      "hello worl\n",
      "h\n",
      "llo\n",
      "llo world\n",
      "hello worldhello world\n"
     ]
    }
   ],
   "source": [
    "str=\"hello world\"\n",
    "print (str)          # 输出字符串\n",
    "print (str[0:-1])    # 输出第一个到倒数第二个的所有字符\n",
    "print (str[0])       # 输出字符串第一个字符\n",
    "print (str[2:5])     # 输出从第三个开始到第五个的字符\n",
    "print (str[2:])      # 输出从第三个开始的后的所有字符\n",
    "print (str * 2)      # 输出字符串两次"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Python 使用反斜杠(\\)转义特殊字符，如果你不想让反斜杠发生转义，可以在字符串前面添加一个 r，表示原始字符串："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "hello\n",
      "i\tao\n"
     ]
    }
   ],
   "source": [
    "print('hello\\ni\\tao')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "hello\\ni\\tao\n"
     ]
    }
   ],
   "source": [
    "print(r'hello\\ni\\tao')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "另外，反斜杠(\\\\)可以作为续行符，表示下一行是上一行的延续。也可以使用 \"\"\"...\"\"\" 或者 '''...''' 跨越多行。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**注意:**\n",
    "1. 反斜杠可以用来转义，使用r可以让反斜杠不发生转义。\n",
    "2. 字符串可以用+运算符连接在一起，用*运算符重复。\n",
    "3. Python中的字符串有两种索引方式，从左往右以0开始，从右往左以-1开始。\n",
    "4. Python中的字符串不能改变。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# List（列表）\n",
    "* List（列表） 是 Python 中使用最频繁的数据类型。\n",
    "* 列表可以完成大多数集合类的数据结构实现。列表中元素的类型可以不相同，它支持数字，字符串甚至可以包含列表（所谓嵌套）。\n",
    "* 列表是写在方括号([])之间、用逗号分隔开的元素列表。\n",
    "* 和字符串一样，列表同样可以被索引和截取，列表被截取后返回一个包含所需元素的新列表。\n",
    "* 列表截取的语法格式如下：\n",
    "> 变量[头下标:尾下标]\n",
    "* 索引值以 0 为开始值，-1 为从末尾的开始位置。\n",
    "* 加号（+）是列表连接运算符，星号（*）是重复操作。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['abcd', 786, 2.23, 'hello', 70.2]\n",
      "abcd\n",
      "[786, 2.23]\n",
      "[2.23, 'hello', 70.2]\n",
      "[123, 'hello', 123, 'hello']\n",
      "['abcd', 786, 2.23, 'hello', 70.2, 123, 'hello']\n"
     ]
    }
   ],
   "source": [
    "list = [ 'abcd', 786 , 2.23, 'hello', 70.2 ]\n",
    "tinylist = [123, 'hello']\n",
    " \n",
    "print (list)            # 输出完整列表\n",
    "print (list[0])         # 输出列表第一个元素\n",
    "print (list[1:3])       # 从第二个开始输出到第三个元素\n",
    "print (list[2:])        # 输出从第三个元素开始的所有元素\n",
    "print (tinylist * 2)    # 输出两次列表\n",
    "print (list + tinylist) # 连接列表"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "与Python字符串不一样的是，列表中的元素是可以改变的："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 2, 3, 4, 5, 11]\n"
     ]
    }
   ],
   "source": [
    "ls=[1,2,3,4,5,6]\n",
    "ls[-1]=11\n",
    "print(ls)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 2, 12]\n"
     ]
    }
   ],
   "source": [
    "ls[2:5]=[12]\n",
    "print(ls)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**注意：**\n",
    "1. List写在方括号之间，元素用逗号隔开。\n",
    "2. 和字符串一样，list可以被索引和切片。\n",
    "3. List可以使用+操作符进行拼接。\n",
    "4. List中的元素是可以改变的。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Tuple（元组）\n",
    "* 元组（tuple）与列表类似，不同之处在于元组的元素不能修改。元组写在小括号 () 里，元素之间用逗号隔开。\n",
    "* 元组中的元素类型也可以不相同："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "('abcd', 786, 2.23, 'hello', 70.2)\n",
      "abcd\n",
      "(786, 2.23)\n",
      "(2.23, 'hello', 70.2)\n",
      "(123, 'hello', 123, 'hello')\n",
      "('abcd', 786, 2.23, 'hello', 70.2, 123, 'hello')\n"
     ]
    }
   ],
   "source": [
    "tuple = ( 'abcd', 786 , 2.23, 'hello', 70.2  )\n",
    "tinytuple = (123, 'hello')\n",
    "print (tuple)             # 输出完整元组\n",
    "print (tuple[0])          # 输出元组的第一个元素\n",
    "print (tuple[1:3])        # 输出从第二个元素开始到第三个元素\n",
    "print (tuple[2:])         # 输出从第三个元素开始的所有元素\n",
    "print (tinytuple * 2)     # 输出两次元组\n",
    "print (tuple + tinytuple) # 连接元组"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "string、list和tuple都属于sequence（序列）。\n",
    "**注意：**\n",
    "1. 与字符串一样，元组的元素不能修改。\n",
    "2. 元组也可以被索引和切片，方法一样。\n",
    "3. 注意构造包含0或1个元素的元组的特殊语法规则。\n",
    "4. 元组也可以使用+操作符进行拼接。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "() (1,) 1 True\n"
     ]
    }
   ],
   "source": [
    "tup=()\n",
    "tup1=(1,)\n",
    "tup2=(1)\n",
    "x=1\n",
    "print(tup,tup1,tup2,tup2==x)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Set（集合）\n",
    "* 集合（set）是一个无序不重复元素的序列。\n",
    "* 基本功能是进行成员关系测试和删除重复元素。\n",
    "* 可以使用大括号 { } 或者 set() 函数创建集合，注意：创建一个空集合必须用 set() 而不是 { }，因为 { } 是用来创建一个空字典。\n",
    "* 创建格式：\n",
    "> parame = {value01,value02,...} <br>  或者 <br> set(value)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'Mary', 'Jim', 'Rose', 'Tom', 'Jack'}\n",
      "Rose 在集合中\n",
      "a: {'c', 'd', 'a', 'b', 'r'}\n",
      "b; {'c', 'a', 'z', 'l', 'm'}\n",
      "a-b: {'d', 'r', 'b'}\n",
      "a|b: {'c', 'd', 'a', 'z', 'b', 'l', 'm', 'r'}\n",
      "a&b: {'c', 'a'}\n",
      "a^b: {'z', 'm', 'd', 'l', 'b', 'r'}\n"
     ]
    }
   ],
   "source": [
    "student = {'Tom', 'Jim', 'Mary', 'Tom', 'Jack', 'Rose'}\n",
    " \n",
    "print(student)   # 输出集合，重复的元素被自动去掉\n",
    " \n",
    "# 成员测试\n",
    "if('Rose' in student) :\n",
    "    print('Rose 在集合中')\n",
    "else :\n",
    "    print('Rose 不在集合中')\n",
    " \n",
    " \n",
    "# set可以进行集合运算\n",
    "a = set('abracadabra')\n",
    "b = set('alacazam')\n",
    " \n",
    "print(\"a:\",a)\n",
    "print('b;',b)\n",
    "print(\"a-b:\",a - b)     # a和b的差集\n",
    "print(\"a|b:\",a | b)     # a和b的并集\n",
    " \n",
    "print(\"a&b:\",a & b)     # a和b的交集\n",
    " \n",
    "print(\"a^b:\",a ^ b)     # a和b中不同时存在的元素\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Dictionary（字典）\n",
    "* 字典（dictionary）是Python中另一个非常有用的内置数据类型。\n",
    "* 列表是有序的对象集合，字典是无序的对象集合。两者之间的区别在于：字典当中的元素是通过键来存取的，而不是通过偏移存取。\n",
    "* 字典是一种映射类型，字典用\"{ }\"标识，它是一个无序的键(key) : 值(value)对集合。\n",
    "* 键(key)必须使用不可变类型。\n",
    "* 在同一个字典中，键(key)必须是唯一的。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 59,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "hello world {'name': '量化投资', 'type': 'learn', 'aim': 'trade'} dict_keys(['name', 'type', 'aim']) dict_values(['量化投资', 'learn', 'trade'])\n"
     ]
    }
   ],
   "source": [
    "dict1={}\n",
    "dict1[1]='hello'\n",
    "dict1[2]='world'\n",
    "dict2={'name':'量化投资','type':'learn',\"aim\":'trade'}\n",
    "print(dict1[1],dict1[2],dict2,dict2.keys(),dict2.values())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 63,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{1: 1, 2: 3}\n",
      "{'wo': 1, 'ni': 2, 'ta': 3, 'dajia': 111}\n"
     ]
    }
   ],
   "source": [
    "dic=dict([(1,1),[2,3]])\n",
    "print(dic)\n",
    "dic=dict(wo=1,ni=2,ta=3,dajia=111)\n",
    "print(dic)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**注意：**\n",
    "1. 字典是一种映射类型，它的元素是键值对。\n",
    "2. 字典的关键字必须为不可变类型，且不能重复。\n",
    "3. 创建空字典使用 { }。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Python数据类型转换"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.3-final"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}